<template>
  <transition name="showHeader">
    <div v-if="visible" class="header-animat">
      <div class="layout-header header-side-opened">
        <div class="header">
          <i class="trigger icon iconfont icon-collapsed" @click="toggle"></i>
          <user-menu ></user-menu>
        </div>
      </div>
    </div>
  </transition>
</template>

<script>
import UserMenu from '../tools/UserMenu'

export default {
  name: 'GlobalHeader',
  components: {
    UserMenu
  },
  props: {
    mode: {
      type: String,
      // sidemenu, topmenu
      default: 'sidemenu'
    },
    theme: {
      type: String,
      required: false,
      default: 'dark'
    },
    collapsed: {
      type: Boolean,
      required: false,
      default: false
    },
    device: {
      type: String,
      required: false,
      default: 'desktop'
    }
  },
  data () {
    return {
      visible: true,
      oldScrollTop: 0
    }
  },
  mounted () {
    document.addEventListener('scroll', this.handleScroll, { passive: true })
  },
  methods: {
    handleScroll () {
      if (!this.autoHideHeader) {
        return
      }

      const scrollTop = document.body.scrollTop + document.documentElement.scrollTop
      if (!this.ticking) {
        this.ticking = true
        requestAnimationFrame(() => {
          if (this.oldScrollTop > scrollTop) {
            this.visible = true
          } else if (scrollTop > 300 && this.visible) {
            this.visible = false
          } else if (scrollTop < 300 && !this.visible) {
            this.visible = true
          }
          this.oldScrollTop = scrollTop
          this.ticking = false
        })
      }
    },
    toggle () {
      this.$emit('toggle')
    }
  },
  beforeDestroy () {
    document.body.removeEventListener('scroll', this.handleScroll, true)
  }
}
</script>

<style lang="scss">
.header-animat {
  position: relative;
  z-index: 105;
}
.showHeader-enter-active {
  transition: all 0.25s ease;
}
.showHeader-leave-active {
  transition: all 0.5s ease;
}
.showHeader-enter, .showHeader-leave-to {
  opacity: 0;
}
.header {
  color: rgba(0, 0, 0, 0.65);
  .trigger {
    font-size: 20px;
    line-height: 50px;
    padding: 15px 24px;
    cursor: pointer;
    -webkit-transition: color 0.3s;
    transition: color 0.3s;
  }
}
</style>
